/**
 * Copyright (c) 2016 Lenovo Group
 * All Rights Reserved.
 * 
 * Project Name:app-gateway
 * Create Time: 2016年1月18日 下午4:14:27
 */
package com.lenovo.lmrp.server.appgateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beust.jcommander.JCommander;
import com.lenovo.lmrp.base.ABaseMain;
import com.lenovo.lmrp.server.appgateway.util.AppCache;
import com.lenovo.lmrp.server.appgateway.util.AppMQClient;
import com.lenovo.lmrp.server.appgateway.util.PropertiesUtil;
import com.lenovo.sdc.framework.util.AttributeBean;

/**
 * @author zhangzhenga
 *
 */
public class AppGatewayServer extends ABaseMain {

    private final static Logger logger = LoggerFactory.getLogger(AppGatewayServer.class);

    private AppGatewayService gatewayService;
    private RestClientFactory restClientFactory;
    private AppCache appCache;
    private AppMQClient appMQClient;

    public static void main(String[] args) {
        AppGatewayServer server = new AppGatewayServer();
        JCommander commander = server.parseCommand(args);

        try {
            server.initialize();
            logger.info("App Gateway Server startup");
        }
        catch (Exception exp) {
            System.out.println("failed to initialize App Gateway Server because " + exp.getMessage());
            exp.printStackTrace();
            commander.usage();
            System.exit(0);
        }
    }

    @Override
    public void initialize() throws Exception {
        // load configuration parameter from appgateway.properties file
        AttributeBean attributes = loadProperties("appgateway.properties");

        // create RestClient factory that used to create client of assigntrace and itsmgateway
        restClientFactory = new RestClientFactory(attributes);

        // setup cache
        appCache = new AppCache(attributes);
        // create MQ client
        appMQClient = new AppMQClient(attributes);
        //init properties
        PropertiesUtil.setAttributes(attributes);
        
        // create App Gateway RESTFul Server and deploy service
        gatewayService = new AppGatewayService(attributes);
        gatewayService.start();
    }
    
    public void destroy(){
        this.gatewayService.stop();
    }
}
